Process Scheduling

Processor manager uses scheduling algorithms to despatch processes

Non-pre-emptive scheduling:

The burst time of a process -> time taken on the CPU before it blocks/terminates
The arrival time of a process -> time a process is first put into the ready state
The turnaround time is the time when a process terminated minus the time when it arrived in the process queue
The wait time is a process' turnaround time minus its burst time

Scheduling Policies

OS will have a policy according to some criteria

Scheduling Algorithms

First Come First Served

Easiest algorithm to implement and understand
Non-pre-emptive algorithm that deals with processes according to their arrival time

Lets say, three processes came in this order, with their respective burst times:
P1 -> 13ms
P2 -> 5ms
P3 -> 1ms

![[Pasted image 20250516103739.png]]
This process schedule can be shown as a Gantt chart, showing cumulative execution time
Average wait time can be calculated

Order of arrival can have a dramatic effect on average wait time
For example, if the same processes arrived in the order:

Advantages

Disadvantages

Better performance could be achieved if schedule could be ordered so that large processes always go last

Shortest Job First

Non-pre-emptive algorithm that deals with processes according to their burst time

For example, no matter the arrival time, if the ready processes and burst times are for example:

Advantages

Disadvantages

In general, non-pre-emptive algorithms are not suitable in modern OSs

Shortest Remaining Time First

Processes are being created all the time

Advantages

Disadvantages

A better approach is to use a pre-emptive scheduling algorithm with a fixed time slice

Priority Scheduling

A pre-emptive algorithm that gives preferential treatment to important processes

Priorities can be set by the user or assigned by the OS depending on whatever characteristic is most important

Let's say we have 5 processes in the ready queue:

Higher numbers mean higher priority (0 is least important)
These processes would be scheduled as follows:
![[Pasted image 20250516105746.png]]
Note that even though P1 and P4 both had priority 3, P1 came first as it was queued earlier

Average wait time

Advantages

Disadvantages

Commonly used in real-time OSs where processes must meet deadlines

Starvation can be solved by process aging

Round Robin Scheduling

A pre-emptive algorithm that gives a set amount of CPU time to each process
Time slice (quantum) is typically between 10ms and 100ms depending on OS and need

Similar to FCFS but processes can be interrupted before they terminate or block

Ready state is treated as a circular FIFO queue

When a process is placed onto the CPU, it will either

If the burst time is less than the quantum time:

For example, with the processes:

In the example, assume all processes arrived at time 0
Consider P2:

Average turnaround time is

Advantages

Disadvantages

If quantum is too big, behaves like FCFS (but shorter processes executed faster)
If quantum is too small, behaves like SJF (but longer processes executed faster)

Windows Priority Scheduling Algorithm

Windows uses the Priority Round Robin algorithm
Priority values range from 0 to 31
Every new process is given one of five base priorities

Linux Priority Scheduling Algorithm

Linus also assigns a priority to each process

Linux uses an algorithm called Completely Fair Scheduling (similar to Round Robin)